package com.hzx.controller;

import com.hzx.pojo.User;
import com.hzx.service.UserInformationService;
import com.hzx.until.SessionUntil;
import com.hzx.until.StringUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class IndexController {

    @Autowired
    UserInformationService userInformationService ;

    @RequestMapping(value = {"/","/index.html"},method = RequestMethod.GET)
    public String index(HttpServletRequest request , HttpServletResponse response){
        //boolean have = haveCookie(request,response) ;
        if(false){
            return "home";
        }else{
            //删除cookie
            Cookie phonecookie = new Cookie("userphone","") ;
            Cookie pwdcookie = new Cookie("userphone","") ;
            pwdcookie.setMaxAge(0);
            phonecookie.setMaxAge(0);
            response.addCookie(pwdcookie);
            response.addCookie(pwdcookie);
            return "index" ;
        }
    }

    //登录
    @RequestMapping(value = "/user/login",method = RequestMethod.GET)
    public String login(@RequestParam String phone, @RequestParam String password, HttpServletResponse response, HttpServletRequest request, Model model){

        if(StringUntil.isNullOrEmpty(phone)){
            return "redirect:/user/login";
        }
        //密码不能为空
        password = StringUntil.userPasswordFormat(password) ;
        if(StringUntil.isNullOrEmpty(password)){
            //重定向
            return "redirect:/user/login" ;
        }

        //获取通行证
        password = StringUntil.strEncrypt(password) ;
        boolean id = getId(phone,password,request,response);

        if(!id){
            return "redirect:/";
        }
        return "home" ;
    }

    //注册
    @RequestMapping(value ="/user/resigter",method = RequestMethod.GET)
    public String regist(@RequestParam String name,@RequestParam String phone,@RequestParam String password,HttpServletRequest request){


        User user = new User() ;
        user.setName(name);
        user.setPhone(phone);
        user.setPassword(StringUntil.strEncrypt(password));
        user.setPictureUrl("/img/UserHead/headpicturedefalut.jpg");
        int ans = userInformationService.insertBySelective(user) ;
        System.out.println(ans);
        if(ans==1){
            System.out.println("直接注册成功");
            return "index" ;
        }else{
            return "error" ;
        }
    }

    boolean haveCookie(HttpServletRequest request,HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        String userPhone = null ;
        String userPwd = null ;
        for (Cookie cookie : cookies) {
            String cookiename = cookie.getName() ;
            if(cookie.getName()=="userphone"){
                userPhone = cookie.getValue() ;
            }else if(cookie.getName()=="userpwd"){
                userPwd = cookie.getValue() ;
            }
        }
        if(StringUntil.isNullOrEmpty(userPhone)&&StringUntil.isNullOrEmpty(userPwd)){
            return getId(userPhone,userPwd,request,response) ;
        }
        return  false ;
    }

    //用ajax来验证手机号是否已经被注册
    @RequestMapping("/regist/checkemail")
    @ResponseBody
    public boolean registCheckPhoneUsed(@RequestParam String phone){
        return StringUntil.isNullOrEmpty(userInformationService.getUidByPhone(phone)) ? true : false ;
    }

    private boolean getId(String phone, String password, HttpServletRequest request,HttpServletResponse response ) {
        Integer uid = userInformationService.getUidByPhone(phone);
        if(StringUntil.isNullOrEmpty(uid)||uid==0){
            return false ;
        }
        User user = userInformationService.getUserByUid(uid) ;
        if(null==user){
            return false ;
        }
        //密码与数据库中对应
        if(!password.equals(user.getPassword())){
            return false ;
        }

        //获取session
        request.getSession().setAttribute("user",user);
        request.getSession().setAttribute("uid",uid);

        //返回cookie
        Cookie phoneCookie = new Cookie("userphone",user.getPhone()) ;
        Cookie pwdCookie = new Cookie("userpwd",user.getPassword()) ;
        phoneCookie.setMaxAge(24*3600*15);
        pwdCookie.setMaxAge(24*3600*15);
        response.addCookie(phoneCookie);
        response.addCookie(pwdCookie);

        //服务器中记录登陆时间
        SessionUntil.saveSession(uid);
        return true;
    }
}
